/*
============================================================================
 Name        : AudioscrobblerEngine.h
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CAudioscrobblerEngine declaration
============================================================================
*/

#ifndef __AUDIOSCROBBLERENGINE_H__
#define __AUDIOSCROBBLERENGINE_H__

// INCLUDES
#include <e32std.h>
#include <e32base.h>

#include "MXmlObserver.h"		//MXmlObserver
#include "Connection.h"			//MConnectionObserver

class TPlaylist;
class TTrack;

class MAudioscrobblerObserver
{
public:
	virtual void PlaylistRequestedL(TPlaylist* aPlayList) = 0;
	virtual void RunError(const TDesC& aErrorDesc) = 0;
};


class CAudioscrobblerEngine :	public CBase,
								public MXmlObserver,
								public MConnectionObserver

{
public: // Constructors and destructor
	~CAudioscrobblerEngine();
	static CAudioscrobblerEngine* NewL(MAudioscrobblerObserver& aObserver);
	static CAudioscrobblerEngine* NewLC(MAudioscrobblerObserver& aObserver);

private:
	CAudioscrobblerEngine(MAudioscrobblerObserver& aObserver);
	void ConstructL();


private: //from MXmlObserver
	void StartElementRead(const TDesC& aNodeName, const RArray<TXmlAttribute> aAttributes);
	void EndElementRead(const TDesC& aNodeName, const TDesC& aData);


private: //from MConnectionObserver
	void ResponseRequestedL(TDesC8& aResponse);
	void StreamingRequestedL(TDesC8& aChunk) {};
	void StreamingEndedL() {};
	void ResponseErrorL(TInt aError);


public: //new functions
	void RequestPlaylistL(const TDesC& aUsername, const TDesC& aPassword);


private: //new functions
	void XspfL();
	void ParseHandshakeL(TDesC8& aResponse);
	void ParseLineL(TDesC8& aLine);
	TBool GetValueL(TDesC8& aLine, const TDesC8& aKey, TDes* value);
	void ParseXmlL(TDesC8& aResponse);


private: //data
	enum TScrobblerRequest
	{
		EScrobblerUndefined,
		EScrobblerHandshake,
		EScrobblerXspf
	} iRequestType;

	//Observer
	MAudioscrobblerObserver& iObserver;

	//Reference to connection
	CConnection* iConnection;

	//Handshake info
	TBuf<32> iSession;
	TBuf<255> iErrorDesc;

	//Metadata
	TPlaylist* iPlaylist;
	TTrack* iCurrentTrack;
};

#endif // __AUDIOSCROBBLERENGINE_H__
